﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace UIControls
{

    public enum LocationEnum
    {
        Left = 1,
        Right = 2,
        Top = 3,
        Buttom = 4
    }


    public class Resizer : Thumb
    {

        /// <summary>
        /// Gets/Sets TargetElement
        /// 操作目标对象
        /// </summary>
        public FrameworkElement TargetElement
        {
            get { return (FrameworkElement)GetValue(TargetElementProperty); }
            set { SetValue(TargetElementProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TargetElement.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetElementProperty =
            DependencyProperty.Register("TargetElement", typeof(FrameworkElement), typeof(Resizer), new UIPropertyMetadata(null, OnTargetElementChanged));

        /// <summary>
        /// 目标操作对象的容器，只允许大小范围在容器内容
        /// </summary>
        public FrameworkElement ContainerElement
        {
            get { return (FrameworkElement)GetValue(ContainerElementProperty); }
            set { SetValue(ContainerElementProperty, value); }
        }

        public static readonly DependencyProperty ContainerElementProperty =
            DependencyProperty.Register("ContainerElement", typeof(FrameworkElement), typeof(Resizer));


        public bool LockZoom { get; set; }

        public FrameworkElement Box
        {
            get { return (FrameworkElement)GetValue(BoxProperty); }
            set { SetValue(BoxProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TargetElement.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BoxProperty =
            DependencyProperty.Register("Box", typeof(FrameworkElement), typeof(Resizer));


        private static void OnTargetElementChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            return;
        }

        private LocationEnum location;
        public LocationEnum Location
        {
            get
            {
                return this.location;
            }
            set
            {
                this.location = value;
                this.Template = this.TryFindResource("res2") as ControlTemplate;
            }
        }

        static Resizer()
        {
            //DefaultStyleKeyProperty.OverrideMetadata(typeof(Resizer), new FrameworkPropertyMetadata(typeof(Resizer)));
        }

        public Resizer()
        {
            this.DragStarted += new DragStartedEventHandler(Resizer_DragStarted);
            this.DragDelta += new DragDeltaEventHandler(Resizer_DragDelta);
            this.DragCompleted += new DragCompletedEventHandler(Resizer_DragCompleted);
        }


        private void Resizer_DragStarted(object sender, DragStartedEventArgs e)
        {

            if (this.Box != null && this.Box is ListBox && this.TargetElement is ListBoxItem)
            {
                ((ListBox)this.Box).SelectedItem = this.TargetElement.DataContext;
            }

            //this.Background = Brushes.Blue;
        }

        private void Resizer_DragCompleted(object sender, DragCompletedEventArgs e)
        {
           // this.Background = Brushes.Orange;
            
        }

        private void Resizer_DragDelta(object sender, DragDeltaEventArgs e)
        {
            double yadjust = this.TargetElement.Height + e.VerticalChange;
            double xadjust = this.TargetElement.Width + e.HorizontalChange;


            double maxWidth = this.ContainerElement.ActualWidth - Canvas.GetLeft(this.TargetElement);
            double maxHeight = this.ContainerElement.ActualHeight - Canvas.GetTop(this.TargetElement);

            if (maxHeight > this.TargetElement.MaxHeight) maxHeight = this.TargetElement.MaxHeight;

            double minWidth = this.TargetElement.MinWidth;
            double minHeight = this.TargetElement.MinHeight;

            if (yadjust > maxHeight) yadjust = maxHeight;
            if (xadjust > maxWidth) xadjust = maxWidth;

            if (yadjust < minHeight) yadjust = minHeight;
            if (xadjust < minWidth) xadjust = minWidth;

            if ((xadjust >= 0) && (yadjust >= 0))
            {

                if (this.LockZoom == true)
                {
                    string content = (this.TargetElement as ListBoxItem).Content.ToString();
                    if (content.Length == 10)
                    {
                        xadjust = yadjust * 3.8 - 30;
                    }
                    else if (content.Length == 5)
                    {
                        xadjust = yadjust * 2 - 10;
                    }
                    else if (content.Length == 3)
                    {
                        xadjust = yadjust * 2 + (yadjust - 80) / 6;
                    }
                    
                }

                this.TargetElement.Width = Math.Round(xadjust);
                this.TargetElement.Height = Math.Round(yadjust);
            }
                
        }
    }
}
